package com.server.user;

import com.dao.user.UserMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.model.po.TokenData;
import com.model.po.User;
import com.model.po.UserGroup;
import com.model.pojo.RepCode;
import com.model.pojo.RepEntity;
import com.model.vo.configVo.LoginInfo;
import com.model.vo.configVo.UserGroupInfo;
import com.model.vo.configVo.UserGroupSetInfo;
import com.util.EncryUtil;
import com.util.JwtTokenUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

@Service
public class UserService {
    @Autowired
    JwtTokenUtil jwtTokenUtil;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ObjectMapper objectMapper;

    public RepEntity login(LoginInfo loginInfo){
        String userName = loginInfo.getUserName();
        String pws = loginInfo.getPassWord();

        if ("".equals(userName) || "".equals(pws)){
            return new RepEntity(RepCode.NoUser);
        }

        String column = "em_mobile";
        //查询uas系统中的用户信息
        User user = userMapper.getUserMess(userName, column);
        //判断登录模式
        if(user == null){
            column = "em_code";
            user = userMapper.getUserMess(userName, column);
            if (user == null){
                return new RepEntity(RepCode.NoUser);
            }
        }

        String password = user.getPassWord();

        String pw = EncryUtil.decryptPassword(password);

        if (!pws.equals(pw)){
            return new RepEntity(RepCode.NoUser);
        }

        System.out.println("user:" + user);
        user.setPassWord(pw);
        user.setUserName(userName);
        //判断uas系统的用户是否再bi中存在
        String name = userMapper.getUserName(userName);
        if (name == null){
            //不存在则插入
            userMapper.addUser(user);
        }else {
            //存在则更新
            userMapper.updateUser(user);
            int userId = userMapper.getUserId(userName);
            user.setId(userId);
        }

        user.setRole(userMapper.getRole(user.getId()));

        TokenData tokenData = jwtTokenUtil.createToke(user);
        tokenData.setUser(user);
        userMapper.updateToken(tokenData.getToken(),user.getId());
        return new RepEntity(RepCode.success, tokenData);
    }

    /*
    更新用户信息
     */
    public RepEntity updateUser(User user){
        userMapper.updateUser(user);
        return new RepEntity(RepCode.success);
    }


    public RepEntity login1(LoginInfo loginInfo){
        String userName = loginInfo.getUserName();
        User user = userMapper.getLogin(userName);
        System.out.println("user:" + user);
        if ("".equals(user) || user == null || "".equals(userName) || !userName.equals(user.getUserName())){
            return new RepEntity(RepCode.NoUser);
        }
        String passWord = user.getPassWord();
        if (!passWord.equals(loginInfo.getPassWord())){
            System.out.println("....passw:" + passWord + "ps:" + loginInfo.getPassWord());
            return new RepEntity(RepCode.NoUser);
        }

        TokenData tokenData = jwtTokenUtil.createToke(user);
        tokenData.setUser(user);
        userMapper.updateToken(tokenData.getToken(),user.getId());
        return new RepEntity(RepCode.success, tokenData);
    }


    /*
    查询用户列表
     */
    public RepEntity getUserList(){
        List<User> userList = userMapper.getUserList();
        return new RepEntity(RepCode.success, userList);
    }

    /*
    查询匹配用户
     */
    public RepEntity getNameList(String str){
        String value = null;
        String v = null;
        try {
            v = objectMapper.readValue(str, String.class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        value = "like '%" + v + "%'";
        System.out.println("value:" + value);

        List<User> userName = userMapper.getNameList(value);
        System.out.println("userName:" + userName);
        return new RepEntity(RepCode.success, userName);
    }


    /*
    创建用户组
     */
    public RepEntity createUserGroup(UserGroupInfo userGroupInfo){
        UserGroup userGroup = new UserGroup();
        BeanUtils.copyProperties(userGroupInfo,userGroup);
        if (userGroup.getUserGroupNote() == null){
            userGroup.setUserGroupNote("");
        }
        userMapper.insUserGroup(userGroup);
        return new RepEntity(RepCode.success, userGroup.getId());
    }

    /*
    更新用户组
     */
    public  RepEntity updateUserGroup(UserGroupInfo userGroupInfo){
        UserGroup userGroup = new UserGroup();
        BeanUtils.copyProperties(userGroupInfo,userGroup);
        userMapper.updateUserGroup(userGroup);
        return new RepEntity(RepCode.success);
    }

    /*
    查询用户组
     */
    public RepEntity getUserGroup(){
        List<UserGroup> userGroup= userMapper.getUserGroup();
        return new RepEntity(RepCode.success, userGroup);
    }

    /*
    删除用户组
     */
    public RepEntity delUserGroup(List<Integer> idList){
        userMapper.delUserGroup(idList);
        return new RepEntity(RepCode.success);
    }

    /*
    添加用户到用户组
     */
    public RepEntity setUserInto(UserGroupSetInfo userGroupSetInfo){
        int userGroupId = userGroupSetInfo.getUserGroupId();
        List<Integer> userList = userGroupSetInfo.getUserList();
        Iterator isList = userList.iterator();
        while (isList.hasNext()){
            int userId = (int) isList.next();
            int count = userMapper.getUserCountInGroup(userId, userGroupId);
            if (count > 0){
                return new RepEntity(RepCode.UserIn);
            }else {
                userMapper.setUserInto(userId, userGroupId);
            }
        }
        return new RepEntity(RepCode.success);
    }

    /*
    查询用户组下的用户信息
     */
    public RepEntity getUserInGroup(int id){
        List<User> user = userMapper.getGroupUserList(id);
        return new RepEntity(RepCode.success, user);
    }

    /*
    删除用户组下的用户
     */
    public RepEntity delUserInGroup(UserGroupSetInfo userGroupSetInfo){
        List<Integer> list = userGroupSetInfo.getUserList();
        int userGroupId = userGroupSetInfo.getUserGroupId();
        Iterator isList = list.iterator();
        while (isList.hasNext()){
            int userId = (int) isList.next();
            userMapper.delUserInGroup(userId, userGroupId);
        }
        return new RepEntity(RepCode.success);
    }
}
